var http = require('http')
var fs = require('fs')
var server = http.createServer((req, responese) => {
  if (req.url === '/') {
    fs.readdir('../../../', (err, res) => {
      if (err) {
        console.log(err)
        return false
      } else {
        responese.writeHead(200, {'Content-Type': 'text/plain'})
        responese.end(res.toString())
        console.log(res)
      }
    })
  } else {
    var path = req.url
    console.log(path)
    fs.stat(`../../../${path}`, (err, result) => {
      if (err) {
        responese.end('Internet Wrong')
        throw err
      } else {
         fs.readFile(`../../../${path}`, (err, data) => {
          if (err) {
            responese.end('Internet Wrong')
            throw err
          } else {
            responese.writeHead(200, {'Content-Type': 'text/plain'})
            responese.end(data)
          }
        })
      }
    })
   
  }
})
server.listen(3000)

server.on('request', (req, responese) => {
  console.log('request on...')
})

process.on('uncaughtException', () => {
  console.log('got err')
})